<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class UserRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $id = $this->route('user') ? $this->route('user')->id : null; //获取当前需要排除的id
        return [
            'name' => 'required|max:50|unique:users,name,' . $id,
            'email' => 'required|email|max:50|unique:users,email,' . $id,
            'phone' => 'required|regex:/^1\d{10}$/|unique:users,phone,' . $id,
            'password' => ($id ? 'nullable' : 'required') . '|min:8|max:30|regex:/^(?=.*\d)(?=.*[a-zA-Z])(?=.*[^a-zA-Z0-9]).{8,30}$/|confirmed',
            'is_lock' => 'required|boolean',
        ];
    }

    /**
     * Get custom messages for validator errors.
     *
     * @return array
     */
    public function messages()
    {
        return [
            'name.required' => '用户名不能为空.',
            'name.max' => '用户名不能大于50位字符.',
            'name.unique' => '用户名已存在.',
            'email.required' => '邮箱不能为空.',
            'email.email' => '邮箱格式输入有误.',
            'email.max' => '邮箱不能大于50位字符.',
            'email.unique' => '邮箱已存在.',
            'phone.required' => '手机号不能为空.',
            'phone.regex' => '手机号格式输入有误.',
            'phone.unique' => '手机号已存在.',
            'password.required' => '密码不能为空.',
            'password.min' => '用户名不能大于8位字符.',
            'password.max' => '用户名不能大于30位字符.',
            'password.regex' => '密码必须包含字母、数字、特称字符，至少8个字符，最多30个字符！',
            'password.confirmed' => '密码输入不一致',
            'status.required' => '锁定状态不能为空.',
            'status.boolean' => '锁定状态操作有误.',
        ];
    }
}
